from typing import Dict, Type
from .base_reporter import BaseReporter
from ..formatters.text_formatter import TextFormatter
from ..formatters.json_formatter import JSONFormatter
from ..formatters.html_formatter import HTMLFormatter
from ..formatters.csv_formatter import CSVFormatter

class ReporterFactory:
    """报告生成器工厂类"""
    
    _formatters: Dict[str, Type[BaseReporter]] = {
        'txt': TextFormatter,
        'json': JSONFormatter,
        'html': HTMLFormatter,
        'csv': CSVFormatter
    }
    
    @classmethod
    def get_reporter(cls, format_type: str, output_dir: str) -> BaseReporter:
        """获取指定格式的报告生成器
        
        Args:
            format_type: 报告格式类型
            output_dir: 输出目录
            
        Returns:
            BaseReporter: 报告生成器实例
            
        Raises:
            ValueError: 当指定的格式类型无效时
        """
        formatter_class = cls._formatters.get(format_type)
        if not formatter_class:
            raise ValueError(f"不支持的报告格式: {format_type}")
        return formatter_class(output_dir)
    
    @classmethod
    def get_all_reporters(cls, output_dir: str) -> Dict[str, BaseReporter]:
        """获取所有格式的报告生成器
        
        Args:
            output_dir: 输出目录
            
        Returns:
            Dict[str, BaseReporter]: 格式类型到报告生成器的映射
        """
        return {
            format_type: formatter_class(output_dir)
            for format_type, formatter_class in cls._formatters.items()
        }
    
    @classmethod
    def register_formatter(cls, format_type: str, formatter_class: Type[BaseReporter]) -> None:
        """注册新的报告格式化器
        
        Args:
            format_type: 报告格式类型
            formatter_class: 格式化器类
            
        Raises:
            ValueError: 当格式化器类不是BaseReporter的子类时
        """
        if not issubclass(formatter_class, BaseReporter):
            raise ValueError(f"格式化器类 {formatter_class.__name__} 必须继承 BaseReporter")
        cls._formatters[format_type] = formatter_class 